package cn.stronglink.oil;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.loopj.android.http.TextHttpResponseHandler;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.stronglink.oil.entity.CommonResult;
import cn.stronglink.oil.entity.DeviceAlarmInfo;
import cn.stronglink.oil.entity.DeviceRealTimeInfo;
import cn.stronglink.oil.entity.GasHistoryInfo;
import cn.stronglink.oil.entity.GasType;
import cn.stronglink.oil.mq.DeviceGasDataManager;
import cn.stronglink.oil.mq.IMqListener;
import cn.stronglink.oil.mq.MqMessageReceiverClient;
import cn.stronglink.oil.user.UserInfo;
import cn.stronglink.oil.utils.HttpClientUtils;
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.HttpConnection;


public class MainActivity extends Activity {
    private MqMessageReceiverClient mqMessageReceiverClient;
    Gson gson = new Gson();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mqMessageReceiverClient = new MqMessageReceiverClient("/GPVLG22/CN1296661B0010/");
        mqMessageReceiverClient.setMqListener(new IMqListener() {
            @Override
            public void onConnect() {
                Log.i("mq", "通讯onConnect");
            }

            @Override
            public void onDisConnect() {
                Log.i("mq", "通讯onDisConnect");
            }

            @Override
            public void onRecData(DeviceRealTimeInfo realTimeInfo) {

            }

            @Override
            public void onRecAlarmData(DeviceAlarmInfo deviceAlarmInfo) {

            }


        });
        mqMessageReceiverClient.startConnect();
    }

    public void onClick(View view) {
        List<DeviceRealTimeInfo> deviceRealTimeInfos = DeviceGasDataManager.getInstance().queryDeviceGasInfos(5,null);
        if (deviceRealTimeInfos!=null&&deviceRealTimeInfos.size()>0) {
            for (int i = 0; i < deviceRealTimeInfos.size(); i++) {
                DeviceRealTimeInfo deviceRealTimeInfo = deviceRealTimeInfos.get(i);
                Log.i("ssssssssssssss",deviceRealTimeInfos.size()+"----"+ gson.toJson(deviceRealTimeInfo));
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

//        HttpClientUtils.getGasHistoryList("/GPVLG22/CN1296661B0010/",50,1, 10, new TextHttpResponseHandler() {
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//
//            }
//
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, String responseString) {
//                if (statusCode==200&&responseString!=null&&!responseString.isEmpty()){
//                    Log.i("sssss",responseString);
//                    CommonResult<GasHistoryInfo> commonResult= new CommonResult();
//                    commonResult = gson.fromJson(responseString,new com.google.gson.reflect.TypeToken<CommonResult<GasHistoryInfo>>(){}.getType());
//                    if (commonResult.getErrorMessage()==null&&commonResult.isSuccess()){
//                        for (int i=0;i<commonResult.getData().getGasHistorys().size();i++) {
//                            Log.i("result",commonResult.getData().getGasHistorys().get(i).getGasName()+"\n");
//                        }
//                    }
//                }
//            }
//        });

        HttpClientUtils.getGasType(new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if (statusCode==200&&responseString!=null&&!responseString.isEmpty()){
                    CommonResult<List<GasType>> commonResult= new CommonResult();
                    commonResult = gson.fromJson(responseString,new com.google.gson.reflect.TypeToken<CommonResult<List<GasType>>>(){}.getType());
                    if (commonResult.getErrorMessage()==null&&commonResult.isSuccess()){
                        for (int i=0;i<commonResult.getData().size();i++) {
                            Log.i("result",commonResult.getData().get(i).getGasName()+"\n");
                        }
                    }
                }
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
//                byte[] bytes = "Hello World!".getBytes();
//                while (true) {
//                    mqMessageReceiverClient.pulish(bytes);
//                    try {
//                        Thread.sleep(2000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
            }
        }).start();
    }
}
